home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / Miro_Installer.exe / xulrunner / chrome / toolkit.jar / content / mozapps / profile / createProfileWizard.js next >
Encoding:
Text File  |  2005-04-25  |  8.1 KB  |  259 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Mozilla Communicator client code, released
  15.  * March 31, 1998.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998-1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Ben Goodger (30/09/99)
  24.  *   Brant Gurganus (23/03/03)
  25.  *   Stefan Borggraefe (17/10/03)
  26.  *   Benjamin Smedberg <bsmedberg@covad.net> - 8-Apr-2004
  27.  *
  28.  * Alternatively, the contents of this file may be used under the terms of
  29.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  30.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  31.  * in which case the provisions of the GPL or the LGPL are applicable instead
  32.  * of those above. If you wish to allow use of your version of this file only
  33.  * under the terms of either the GPL or the LGPL, and not to allow others to
  34.  * use your version of this file under the terms of the MPL, indicate your
  35.  * decision by deleting the provisions above and replace them with the notice
  36.  * and other provisions required by the GPL or the LGPL. If you do not delete
  37.  * the provisions above, a recipient may use your version of this file under
  38.  * the terms of any one of the MPL, the GPL or the LGPL.
  39.  *
  40.  * ***** END LICENSE BLOCK ***** */
  41.  
  42. const C = Components.classes;
  43. const I = Components.interfaces;
  44.  
  45. const ToolkitProfileService = "@mozilla.org/toolkit/profile-service;1";
  46.  
  47. var gProfileService;
  48. var gProfileManagerBundle;
  49.  
  50. var gDefaultProfileParent;
  51. var gOldProfileName;
  52.  
  53. // The directory where the profile will be created.
  54. var gProfileRoot;
  55.  
  56. // Text node to display the location and name of the profile to create.
  57. var gProfileDisplay;
  58.  
  59. // Called once when the wizard is opened.
  60. function initWizard()
  61.   try {
  62.     gProfileService = C[ToolkitProfileService].getService(I.nsIToolkitProfileService);
  63.     gProfileManagerBundle = document.getElementById("bundle_profileManager");
  64.  
  65.     var dirService = C["@mozilla.org/file/directory_service;1"].getService(I.nsIProperties);
  66.     gDefaultProfileParent = dirService.get("DefProfRt", I.nsIFile);
  67.  
  68.     gOldProfileName = document.getElementById("profileName").value;
  69.  
  70.     // Initialize the profile location display.
  71.     gProfileDisplay = document.getElementById("profileDisplay").firstChild;
  72.     setDisplayToDefaultFolder();
  73.   }
  74.   catch(e) {
  75.     window.close();
  76.     throw (e);
  77.   }
  78. }
  79.  
  80. // Called every time the second wizard page is displayed.
  81. function initSecondWizardPage() 
  82. {
  83.   var profileName = document.getElementById("profileName");
  84.   profileName.select();
  85.   profileName.focus();
  86.  
  87.   // Initialize profile name validation.
  88.   checkCurrentInput(profileName.value);
  89. }
  90.  
  91. const kSaltTable = [
  92.   'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  93.   'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  94.   '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' ];
  95.  
  96. var kSaltString = "";
  97. for (var i = 0; i < 8; ++i) {
  98.   kSaltString += kSaltTable[Math.floor(Math.random() * kSaltTable.length)];
  99. }
  100.  
  101.  
  102. function saltName(aName)
  103. {
  104.   return kSaltString + "." + aName;
  105. }
  106.  
  107. function setDisplayToDefaultFolder()
  108. {
  109.   var defaultProfileDir = gDefaultProfileParent.clone();
  110.   defaultProfileDir.append(saltName(document.getElementById("profileName").value));
  111.   gProfileRoot = defaultProfileDir;
  112.   document.getElementById("useDefault").disabled = true;
  113. }
  114.  
  115. function updateProfileDisplay()
  116. {
  117.   gProfileDisplay.data = gProfileRoot.path;
  118. }
  119.  
  120. // Invoke a folder selection dialog for choosing the directory of profile storage.
  121. function chooseProfileFolder()
  122. {
  123.   var newProfileRoot;
  124.   
  125.   var dirChooser = C["@mozilla.org/filepicker;1"].createInstance(I.nsIFilePicker);
  126.   dirChooser.init(window, gProfileManagerBundle.getString("chooseFolder"),
  127.                   I.nsIFilePicker.modeGetFolder);
  128.   dirChooser.appendFilters(I.nsIFilePicker.filterAll);
  129.   dirChooser.show();
  130.   newProfileRoot = dirChooser.file;
  131.  
  132.   // Disable the "Default Folder..." button when the default profile folder
  133.   // was selected manually in the File Picker.
  134.   document.getElementById("useDefault").disabled =
  135.     (newProfileRoot.parent.equals(gDefaultProfileParent));
  136.  
  137.   gProfileRoot = newProfileRoot;
  138.   updateProfileDisplay();
  139. }
  140.  
  141. // Checks the current user input for validity and triggers an error message accordingly.
  142. function checkCurrentInput(currentInput)
  143. {
  144.   var finishButton = document.documentElement.getButton("finish");
  145.   var finishText = document.getElementById("finishText");
  146.   var canAdvance;
  147.  
  148.   var errorMessage = checkProfileName(currentInput);
  149.  
  150.   if (!errorMessage) {
  151.     finishText.className = "";
  152.     finishText.firstChild.data = gProfileManagerBundle.getString("profileFinishText");
  153.     canAdvance = true;
  154.   }
  155.   else {
  156.     finishText.className = "error";
  157.     finishText.firstChild.data = errorMessage;
  158.     canAdvance = false;
  159.   }
  160.  
  161.   document.documentElement.canAdvance = canAdvance;
  162.   finishButton.disabled = !canAdvance;
  163.  
  164.   updateProfileDisplay();
  165. }
  166.  
  167. function updateProfileName(aNewName) {
  168.   checkCurrentInput(aNewName);
  169.  
  170.   var re = new RegExp("^[a-z0-9]{8}\\." +
  171.                       gOldProfileName.replace(/[|^$()\[\]{}\\+?.*]/g, "\\$&")
  172.                       + '$');
  173.  
  174.   if (re.test(gProfileRoot.leafName)) {
  175.     gProfileRoot.leafName = saltName(aNewName);
  176.     updateProfileDisplay();
  177.   }
  178.   gOldProfileName = aNewName;
  179. }
  180.  
  181. // Checks whether the given string is a valid profile name.
  182. // Returns an error message describing the error in the name or "" when it's valid.
  183. function checkProfileName(profileNameToCheck)
  184. {
  185.   // Check for emtpy profile name.
  186.   if (!/\S/.test(profileNameToCheck))
  187.     return gProfileManagerBundle.getString("profileNameEmpty");
  188.  
  189.   // Check whether all characters in the profile name are allowed.
  190.   if (/([\\*:?<>|\/\"])/.test(profileNameToCheck))
  191.     return gProfileManagerBundle.getFormattedString("invalidChar", [RegExp.$1]);
  192.  
  193.   // Check whether a profile with the same name already exists.
  194.   if (profileExists(profileNameToCheck))
  195.     return gProfileManagerBundle.getString("profileExists");
  196.  
  197.   // profileNameToCheck is valid.
  198.   return "";
  199. }
  200.  
  201. function profileExists(aName)
  202. {
  203.   var profiles = gProfileService.profiles;
  204.   while (profiles.hasMoreElements()) {
  205.     var profile = profiles.getNext().QueryInterface(I.nsIToolkitProfile);
  206.     if (profile.name.toLowerCase() == aName.toLowerCase())
  207.       return true;
  208.   }
  209.  
  210.   return false;
  211. }
  212.  
  213. // Called when the first wizard page is shown.
  214. function enableNextButton()
  215. {
  216.   document.documentElement.canAdvance = true;
  217. }
  218.  
  219. function onFinish() 
  220. {
  221.   var profileName = document.getElementById("profileName").value;
  222.   var profile;
  223.  
  224.   // Create profile named profileName in profileRoot.
  225.   try {
  226.     profile = gProfileService.createProfile(gProfileRoot, null, profileName);
  227.   }
  228.   catch (e) {
  229.     var profileCreationFailed =
  230.       gProfileManagerBundle.getString("profileCreationFailed");
  231.     var profileCreationFailedTitle =
  232.       gProfileManagerBundle.getString("profileCreationFailedTitle");
  233.     var promptService = C["@mozilla.org/embedcomp/prompt-service;1"].
  234.       getService(I.nsIPromptService);
  235.     promptService.alert(window, profileCreationFailedTitle,
  236.                         profileCreationFailed + "\n" + e);
  237.  
  238.     return false;
  239.   }
  240.  
  241.   // window.opener is false if the Create Profile Wizard was opened from the
  242.   // command line.
  243.   if (window.opener) {
  244.     // Add new profile to the list in the Profile Manager.
  245.     window.opener.CreateProfile(profile);
  246.   }
  247.   else {
  248.     // Use the newly created Profile.
  249.     var profileLock = profile.lock(null);
  250.  
  251.     var dialogParams = window.arguments[0].QueryInterface(I.nsIDialogParamBlock);
  252.     dialogParams.objects.insertElementAt(profileLock, 0, false);
  253.   }
  254.  
  255.   // Exit the wizard.
  256.   return true;
  257. }
  258.